package cn.stylefeng.guns.modular.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicLineParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicLineResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.system.entity.monthTotalTmsEntity2;
import cn.stylefeng.guns.modular.system.model.params.DDQSQKFX;
import cn.stylefeng.guns.modular.system.model.params.YDGBFHLFX;
import cn.stylefeng.guns.modular.system.model.params.YDMZLPH;
import cn.stylefeng.guns.modular.system.model.params.YDQSSXZB;
import cn.stylefeng.guns.modular.system.model.params.YDXLCBDCFDFX;
import cn.stylefeng.guns.modular.system.model.params.YDXLCBJFFDFX;
import cn.stylefeng.guns.modular.system.model.params.YDXLZLFX;
import cn.stylefeng.guns.modular.system.model.params.YDZFYFSFX;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchResult;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 查询 TMS面板二  数据
 */
@Controller
@RequestMapping("/panelTms2")
public class panelTmsController2 extends BaseController{

    private String PREFIX = "/panel";

    @Autowired
    private OrderTransportService orderTransportService;

    @Autowired
    private BasicOperateService basicOperateService;
    
    /**
     * 跳转到月度运输线路统计
     */
    @RequestMapping("yueduyunshuxianluzhgibiaoTongji_jl")
    public String yueduyunshuxianluzhgibiaoTongji_jl(Model model,BasicOperateParam basicOperateParam) {
    	List<BasicOperateResult> list = basicOperateService.findTransportListBySpec(basicOperateParam,null);
    	model.addAttribute("olist", list);
        return PREFIX + "/yueduyunshuxianluzhgibiaoTongji_jl.html";
    }
    
    /**
     * 跳转到月度运输指标统计
     */
    @RequestMapping("yueduyunshuZhibiaotongji_jl")
    public String yueduyunshuZhibiaotongji_jl() {
        return PREFIX + "/yueduyunshuZhibiaotongji_jl.html";
    }

    /**
     * 查询项目部
     */
    @ResponseBody
    @RequestMapping("/findOperate")
    public ResponseData findOperate(BasicOperateParam basicOperateParam,String month) {
    	List<BasicOperateResult> list = basicOperateService.findTransportListBySpec(basicOperateParam,month);
    	return ResponseData.success(list);
    }
    
    /**
     * 查询当月总指标
     */
    @ResponseBody
    @RequestMapping("/monthTotalindex2")
    public ResponseData monthTotalindex2(String month,Long operateId,Long deliverId) {
    	monthTotalTmsEntity2 res = new monthTotalTmsEntity2();
    	OrderTransportParam orderTransportParam = new OrderTransportParam();
    	orderTransportParam.setDeliverId(deliverId);
    	Integer monthNewOrder = orderTransportService.sumMonthNewOrderCount2(orderTransportParam,month);//月新订单量
    	res.setZDDL(monthNewOrder+"");
    	
    	OrderTransportParam orderTransportParam2 = new OrderTransportParam();
    	orderTransportParam2.setDeliverId(deliverId);
    	Integer monthYiqianshouOrder = orderTransportService.sumMonthYqianshouOrderCount2(orderTransportParam2,month);//已签收订单量
    	res.setYQSDD(monthYiqianshouOrder+"");
    	
    	//计算签收率    已签收单量/月订单
    	BigDecimal arr = new BigDecimal(100);
    	BigDecimal as = new BigDecimal(0);
    	BigDecimal b1 = new BigDecimal(monthNewOrder);
    	BigDecimal b2 = new BigDecimal(monthYiqianshouOrder);
    	if(b2.compareTo(as) == 1) {
    		BigDecimal monthOrderVolume = b2.divide(b1,4,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    		res.setQSL(monthOrderVolume+"");
    	} else {
    		res.setQSL("0");
    	}
    	
    	OrderTransportParam orderTransportParam3 = new OrderTransportParam();
    	orderTransportParam3.setDeliverId(deliverId);
    	Integer monthFaYunCar = orderTransportService.sumMonthYhuidanOrderCount(orderTransportParam3,month);//已回单订单量
    	
    	//计算 回单上传率    已签收订单量/已回单订单量
    	BigDecimal b11 = new BigDecimal(monthYiqianshouOrder);
    	BigDecimal b22 = new BigDecimal(monthFaYunCar);
    	if(b22.compareTo(as)==1) {
    		BigDecimal monthFaYunCar2 = b22.divide(b11,4,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    		res.setHDSCL(monthFaYunCar2+"");
    	} else {
    		res.setHDSCL("0");
    	}
    	
    	List<DDQSQKFX> DDQSQKFXs = new ArrayList<DDQSQKFX>();//订单签收情况分析
    	List<YDQSSXZB> YDQSSXZBs = new ArrayList<YDQSSXZB>();//月度签收时效占比
    	List<YDMZLPH> YDMZLPHs = new ArrayList<YDMZLPH>();//月度满载率排行
    	List<YDZFYFSFX> YDZFYFSFXs = new ArrayList<YDZFYFSFX>();//月度总发运方数对比分析
    	List<YDGBFHLFX> YDGBFHLFXs = new ArrayList<YDGBFHLFX>();//地图
    	
    	BasicOperateParam basicOperateParam = new BasicOperateParam();
    	basicOperateParam.setDeliverId(deliverId);
		basicOperateParam.setOperateCiywhere(month);
		List<BasicOperateResult> list = basicOperateService.monthTotalQianOrderCount(basicOperateParam);//查询  各项目当月  总签收订单数
		for (BasicOperateResult r : list) {
			orderTransportParam.setOperateId(Long.parseLong(r.getId().toString()));
	    	Integer monthNewOrder2 = orderTransportService.sumMonthNewOrderCount2(orderTransportParam,month);//月新订单量
	    	if(monthNewOrder2 == 0) {
	    		continue;
	    	}
	    	YDGBFHLFX YDGBFHLFX = new YDGBFHLFX();
	    	YDGBFHLFX.setDL(monthNewOrder2+"");
	    	YDGBFHLFX.setXMMC(r.getAbbreviation());
	    	YDGBFHLFXs.add(YDGBFHLFX);
	    	
			BigDecimal sum = new BigDecimal(r.getOperateTel());//签收总单量
			if(sum.compareTo(as) == 1) {
				DDQSQKFX DDQSQKFX = new DDQSQKFX();
				DDQSQKFX.setXMMC(r.getAbbreviation());
				BasicOperateParam basicOperateParam2 = new BasicOperateParam();
				basicOperateParam2.setDeliverId(deliverId);
				basicOperateParam2.setOperateCiywhere(month);
				basicOperateParam2.setId(r.getId());
				BasicOperateResult result = basicOperateService.monthTotalQianOrderCountTimeById(basicOperateParam2);//查询  项目当月  签收订单总用时
				BigDecimal operateTel = new BigDecimal(result.getOperateTel());
				if(operateTel.compareTo(as) == 1) {
					operateTel = operateTel.divide(sum,2,BigDecimal.ROUND_HALF_DOWN);//签收订单总用时 /  总签收订单数 = 平均签收时效
					DDQSQKFX.setDDQSSX(operateTel.toString());
				} else {
					DDQSQKFX.setDDQSSX("0");
				}

				List<BasicOperateResult> fResult = basicOperateService.monthTotalFayunOrderCount(basicOperateParam2);//查询  项目当月  总发运订单数
				BigDecimal fum = new BigDecimal(fResult.get(0).getOperateTel());//发运总单量
				if(fum.compareTo(as) == 1) {
					fum = sum.divide(fum,4,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//总签收订单数 / 发运总单量 * 100  = 订单签收率
					DDQSQKFX.setDDQSL(fum.toString());
				} else {
					DDQSQKFX.setDDQSL("0");
				}

		    	orderTransportParam3.setOperateId(Long.parseLong(r.getId().toString()));
		    	Integer hd = orderTransportService.sumMonthYhuidanOrderCount(orderTransportParam3,month);//已回单订单量
				BigDecimal hum = new BigDecimal(hd);
				if(hum.compareTo(as) == 1) {
					hum = hum.divide(sum,4,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//已回单订单量 / 总签收订单数 * 100  = 回单上传率
					DDQSQKFX.setHDSCL(hum.toString());
				} else {
					DDQSQKFX.setHDSCL("0");
				}
				DDQSQKFXs.add(DDQSQKFX);
				
				YDQSSXZB YDQSSXZB = new YDQSSXZB();
				YDQSSXZB.setXMMC(r.getAbbreviation());
				BasicOperateResult result2 = basicOperateService.monthTotalQianshouOrderCountById(basicOperateParam2);//查询  项目当月  签收订单时间段数
				BigDecimal operateTel2 = new BigDecimal(result2.getOperateTel());
				if(operateTel2.compareTo(as) == 1) {
					operateTel2 = operateTel2.divide(sum,2,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//12h内签收 占比
					YDQSSXZB.setQS12ZB(operateTel2.toString());
				} else {
					YDQSSXZB.setQS12ZB("0");
				}
				BigDecimal operateEmall = new BigDecimal(result2.getOperateEmall());
				if(operateEmall.compareTo(as) == 1) {
					operateEmall = operateEmall.divide(sum,2,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//24h内签收 占比
					YDQSSXZB.setQS24ZB(operateEmall.toString());
				} else {
					YDQSSXZB.setQS24ZB("0");
				}
				BigDecimal operateProvinces = new BigDecimal(result2.getOperateProvinces());
				if(operateProvinces.compareTo(as) == 1) {
					operateProvinces = operateProvinces.divide(sum,2,BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//48h内签收 占比
					YDQSSXZB.setQS48ZB(operateProvinces.toString());
				} else {
					YDQSSXZB.setQS48ZB("0");
				}
				YDQSSXZBs.add(YDQSSXZB);
				
				YDZFYFSFX YDZFYFSFX = new YDZFYFSFX();
				YDZFYFSFX.setXMMC(r.getAbbreviation());
				YDZFYFSFX.setBYYL(month);				
				TDispatchParam tDispatchParam = new TDispatchParam();
				tDispatchParam.setDeptId(r.getDeptId());
				tDispatchParam.setMoney(month);
				TDispatchResult facheci =  tDispatchService.findsumMonthFaYunCarCount(tDispatchParam,Long.parseLong(r.getId().toString()));//合计  项目当月  总发运订单  满载率  方数  车次
				if(facheci != null) {
					YDMZLPH YDMZLPH = new YDMZLPH();
					if(ToolUtil.isNotEmpty(facheci.getFulllv())) {
						BigDecimal operateTel3 = new BigDecimal(facheci.getFulllv());//满载率合计
						BigDecimal operateProvinces2 = new BigDecimal(facheci.getTrainNumber());//发运车次
						if(operateTel3.compareTo(as)==1){
							operateProvinces2 = operateTel3.divide(operateProvinces2,2,BigDecimal.ROUND_HALF_DOWN);//  满载率合计/总发运车次 = 平均满载率
							YDMZLPH.setMZL(operateProvinces2+"");
						} else {
							YDMZLPH.setMZL("0");
						}
					} else {
						YDMZLPH.setMZL("0");
					}
					YDMZLPH.setXMMC(r.getAbbreviation());
					YDMZLPHs.add(YDMZLPH);
					
					if(ToolUtil.isNotEmpty(facheci.getToovolume())) {
						YDZFYFSFX.setBYBQ(facheci.getToovolume());
					} else {
						YDZFYFSFX.setBYBQ("0");
					}
				} else {
					YDZFYFSFX.setBYBQ("0");
				}

				tDispatchParam.setMoney(DateUtil.getYearMonthByMonth(month,-1));
				TDispatchResult facheci2 =  tDispatchService.findsumMonthFaYunCarCount(tDispatchParam,Long.parseLong(r.getId().toString()));//合计  项目当月  总发运订单  满载率  方数  车次
				if(facheci2 != null) {
					if(ToolUtil.isNotEmpty(facheci2.getToovolume())) {
						YDZFYFSFX.setSYBQ(facheci2.getToovolume());
					} else {
						YDZFYFSFX.setSYBQ("0");
					}
				} else {
					YDZFYFSFX.setSYBQ("0");
				}
				YDZFYFSFX.setSYYL(DateUtil.getYearMonthByMonth(month,-1));
				YDZFYFSFXs.add(YDZFYFSFX);
			}
		}

    	List<YDXLZLFX> YDXLZLFXs = new ArrayList<YDXLZLFX>();//月度线路总量分析
    	List<YDXLCBJFFDFX> YDXLCBJFFDFXs = new ArrayList<YDXLCBJFFDFX>();//月度线路成本均方浮动分析
    	List<YDXLCBDCFDFX> YDXLCBDCFDFXs = new ArrayList<YDXLCBDCFDFX>();//月度线路成本吨车浮动分析
    	
		BasicLineParam basicLineParam = new BasicLineParam();
		basicLineParam.setDeliverId(deliverId);
		if(operateId != null){
			basicLineParam.setOperateId(Integer.parseInt(operateId.toString()));
		}
		basicLineParam.setLineName(month);
		List<BasicLineResult> listli =  basicLineService.sumfindListBySpec(basicLineParam);
		for (BasicLineResult r : listli) {
			YDXLZLFX YDXLZLFX = new YDXLZLFX();
			YDXLZLFX.setXL(r.getLineName());
			YDXLZLFX.setZFS(r.getNums());
			YDXLZLFX.setZYF(r.getMoney());
			YDXLZLFXs.add(YDXLZLFX);
			if(r.getPriceMode().equals("元/m³")) {
				YDXLCBJFFDFX YDXLCBJFFDFX = new YDXLCBJFFDFX();
				YDXLCBJFFDFX.setXL(r.getLineName());
				YDXLCBJFFDFX.setCKJ(r.getPrice());
				YDXLCBJFFDFX.setPJJ(r.getAmoney2());
				YDXLCBJFFDFXs.add(YDXLCBJFFDFX);			
			}
			if(r.getPriceMode().equals("元/趟")||r.getPriceMode().equals("元/吨")) {
				YDXLCBDCFDFX YDXLCBDCFDFX = new YDXLCBDCFDFX();
				YDXLCBDCFDFX.setXL(r.getLineName());
				YDXLCBDCFDFX.setCKJ(r.getPrice());
				YDXLCBDCFDFX.setPJJ(r.getAmoney());
				YDXLCBDCFDFXs.add(YDXLCBDCFDFX);	
			}
		}
		res.setDDQSQKFXs(DDQSQKFXs);
		res.setYDQSSXZBs(YDQSSXZBs);
		res.setYDMZLPHs(YDMZLPHs);
		res.setYDZFYFSFXs(YDZFYFSFXs);
		res.setYDXLZLFXs(YDXLZLFXs);
		res.setYDXLCBJFFDFXs(YDXLCBJFFDFXs);
		res.setYDXLCBDCFDFXs(YDXLCBDCFDFXs);
		res.setYDGBFHLFXs(YDGBFHLFXs);
        return ResponseData.success(res);
    }

    @Autowired
    private TDispatchService tDispatchService;

    @Autowired
    private BasicLineService basicLineService;
}
